# Copyright 2010-2025 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@com_google_protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_python//python:proto.bzl", "py_proto_library")

package(default_visibility = ["//visibility:public"])

# Description:
# Home of algorithms used in OR solvers

config_setting(
    name = "on_linux",
    constraint_values = ["@platforms//os:linux"],
)

config_setting(
    name = "on_macos",
    constraint_values = ["@platforms//os:macos"],
)

config_setting(
    name = "on_windows",
    constraint_values = ["@platforms//os:windows"],
)

# OSS solvers
bool_flag(
    name = "with_cbc",
    build_setting_default = False,
)

config_setting(
    name = "use_cbc",
    flag_values = {
        ":with_cbc": "true",
    },
)

bool_flag(
    name = "with_scip",
    build_setting_default = True,
)

config_setting(
    name = "use_scip",
    flag_values = {
        ":with_scip": "true",
    },
)

cc_library(
    name = "binary_search",
    srcs = [],
    hdrs = ["binary_search.h"],
    deps = [
        "//ortools/base",
        "@com_google_absl//absl/functional:function_ref",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/numeric:int128",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "binary_search_test",
    srcs = ["binary_search_test.cc"],
    deps = [
        ":binary_search",
        "//ortools/base:gmock_main",
        "//ortools/base:murmur",
        "@com_google_absl//absl/base:log_severity",
        "@com_google_absl//absl/numeric:int128",
        "@com_google_absl//absl/random",
        "@com_google_absl//absl/random:distributions",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/time",
        "@com_google_benchmark//:benchmark",
    ],
)

cc_library(
    name = "radix_sort",
    srcs = [],
    hdrs = ["radix_sort.h"],
    deps = [
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/numeric:bits",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "radix_sort_test",
    srcs = ["radix_sort_test.cc"],
    copts = select({
        "on_linux": [],
        "on_macos": [],
        "on_windows": ["/Zc:preprocessor"],
        "//conditions:default": [],
    }),
    deps = [
        ":radix_sort",
        "//ortools/base:dump_vars",
        "//ortools/base:gmock_main",
        "//ortools/base:mathutil",
        "//ortools/base:timer",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/random",
        "@com_google_absl//absl/random:bit_gen_ref",
        "@com_google_absl//absl/random:distributions",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
        "@com_google_benchmark//:benchmark",
    ],
)

cc_library(
    name = "duplicate_remover",
    srcs = ["duplicate_remover.cc"],
    hdrs = ["duplicate_remover.h"],
    deps = [
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/numeric:bits",
        "@com_google_absl//absl/random",
        "@com_google_absl//absl/random:distributions",
        "@com_google_absl//absl/types:span",
        "@com_google_protobuf//:protobuf",
    ],
)

# Hungarian algorithm
cc_library(
    name = "hungarian",
    srcs = ["hungarian.cc"],
    hdrs = ["hungarian.h"],
    deps = [
        "//ortools/base",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "hungarian_test",
    size = "medium",
    srcs = ["hungarian_test.cc"],
    deps = [
        ":hungarian",
        "//ortools/base",
        "//ortools/base:gmock_main",
        "//ortools/base:map_util",
        "//ortools/base:types",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/random:distributions",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "adjustable_k_ary_heap",
    hdrs = ["adjustable_k_ary_heap.h"],
    deps = ["@com_google_absl//absl/log:check"],
)

cc_test(
    name = "adjustable_k_ary_heap_test",
    size = "medium",
    srcs = ["adjustable_k_ary_heap_test.cc"],
    deps = [
        ":adjustable_k_ary_heap",
        "//ortools/base:gmock_main",
    ],
)

cc_test(
    name = "adjustable_k_ary_heap_stress_test",
    size = "large",
    timeout = "eternal",
    srcs = ["adjustable_k_ary_heap_stress_test.cc"],
    deps = [
        ":adjustable_k_ary_heap",
        "//ortools/base:gmock_main",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/random",
    ],
)

cc_library(
    name = "knapsack_solver_lib",
    srcs = ["knapsack_solver.cc"],
    hdrs = ["knapsack_solver.h"],
    copts = [] + select({
        ":use_cbc": ["-DUSE_CBC"],
        "//conditions:default": [],
    }) + select({
        ":use_scip": ["-DUSE_SCIP"],
        "//conditions:default": [],
    }),
    deps = [
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
        "//ortools/base:stl_util",
        # We don't link any underlying solver to let the linear_solver_knapsack
        # decide what solvers to include.
        "//ortools/linear_solver",
        "//ortools/sat:cp_model",
        "//ortools/sat:cp_model_cc_proto",
        "//ortools/sat:cp_model_solver",
        "//ortools/util:bitset",
        "//ortools/util:time_limit",
    ],
)

cc_test(
    name = "knapsack_solver_test",
    size = "medium",
    srcs = ["knapsack_solver_test.cc"],
    deps = [
        ":knapsack_solver_lib",  # buildcleaner: keep
        "//ortools/base",
        "//ortools/base:gmock_main",
        "//ortools/util:time_limit",
    ],
)

# Partitioning and splitting of vector<int64_t>.

# query matching library.

# Weighted set covering library.

proto_library(
    name = "set_cover_proto",
    srcs = ["set_cover.proto"],
    deps = [
        "//ortools/util:int128_proto",
    ],
)

cc_proto_library(
    name = "set_cover_cc_proto",
    deps = [":set_cover_proto"],
)

py_proto_library(
    name = "set_cover_py_pb2",
    deps = [":set_cover_proto"],
)

cc_library(
    name = "set_cover_lagrangian",
    srcs = ["set_cover_lagrangian.cc"],
    hdrs = ["set_cover_lagrangian.h"],
    deps = [
        ":adjustable_k_ary_heap",
        ":set_cover_invariant",
        ":set_cover_model",
        "//ortools/base:threadpool",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/synchronization",
    ],
)

cc_library(
    name = "set_cover_model",
    srcs = ["set_cover_model.cc"],
    hdrs = ["set_cover_model.h"],
    deps = [
        ":radix_sort",
        ":set_cover_cc_proto",
        "//ortools/base:intops",
        "//ortools/base:strong_vector",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/numeric:bits",
        "@com_google_absl//absl/random",
        "@com_google_absl//absl/random:distributions",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "set_cover_invariant",
    srcs = ["set_cover_invariant.cc"],
    hdrs = ["set_cover_invariant.h"],
    deps = [
        ":set_cover_cc_proto",
        ":set_cover_model",
        "//ortools/base",
        "//ortools/base:mathutil",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "set_cover_heuristics",
    srcs = ["set_cover_heuristics.cc"],
    hdrs = ["set_cover_heuristics.h"],
    deps = [
        ":adjustable_k_ary_heap",
        ":set_cover_invariant",
        ":set_cover_model",
        "//ortools/base",
        "@com_google_absl//absl/base",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/numeric:bits",
        "@com_google_absl//absl/random:distributions",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "set_cover_mip",
    srcs = ["set_cover_mip.cc"],
    hdrs = ["set_cover_mip.h"],
    deps = [
        ":set_cover_invariant",
        ":set_cover_model",
        "//ortools/linear_solver",
        "//ortools/lp_data:base",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "set_cover_reader",
    srcs = ["set_cover_reader.cc"],
    hdrs = ["set_cover_reader.h"],
    deps = [
        ":set_cover_cc_proto",
        ":set_cover_model",
        "//ortools/base:file",
        "//ortools/util:filelineiter",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/strings:string_view",
    ],
)

cc_binary(
    name = "set_cover_solve",
    srcs = ["set_cover_solve.cc"],
    deps = [
        ":set_cover_heuristics",
        ":set_cover_invariant",
        ":set_cover_model",
        ":set_cover_reader",
        "//ortools/base",
        "//ortools/base:timer",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
    ],
)

cc_test(
    name = "set_cover_test",
    size = "medium",
    timeout = "eternal",
    srcs = ["set_cover_test.cc"],
    deps = [
        ":set_cover_cc_proto",
        ":set_cover_heuristics",
        ":set_cover_invariant",
        ":set_cover_mip",
        ":set_cover_model",
        "//ortools/base:gmock_main",
        "//ortools/base:parse_text_proto",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/strings",
        "@com_google_benchmark//:benchmark",
    ],
)

# Graph automorphism libraries.
cc_library(
    name = "dense_doubly_linked_list",
    hdrs = ["dense_doubly_linked_list.h"],
    deps = [
        "//ortools/base",
    ],
)

cc_test(
    name = "dense_doubly_linked_list_test",
    srcs = ["dense_doubly_linked_list_test.cc"],
    deps = [
        ":dense_doubly_linked_list",
        "//ortools/base:gmock_main",
    ],
)

cc_library(
    name = "dynamic_partition",
    srcs = ["dynamic_partition.cc"],
    hdrs = ["dynamic_partition.h"],
    deps = [
        "//ortools/base:murmur",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "dynamic_partition_test",
    srcs = ["dynamic_partition_test.cc"],
    deps = [
        ":dynamic_partition",
        "//ortools/base:gmock_main",
        "//ortools/base:stl_util",
        "@com_google_absl//absl/random",
        "@com_google_absl//absl/random:bit_gen_ref",
    ],
)

cc_library(
    name = "sparse_permutation",
    srcs = ["sparse_permutation.cc"],
    hdrs = ["sparse_permutation.h"],
    deps = [
        "//ortools/base",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "sparse_permutation_test",
    srcs = ["sparse_permutation_test.cc"],
    deps = [
        ":sparse_permutation",
        "//ortools/base:gmock_main",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/random:distributions",
    ],
)

cc_library(
    name = "dynamic_permutation",
    srcs = ["dynamic_permutation.cc"],
    hdrs = ["dynamic_permutation.h"],
    deps = [
        ":sparse_permutation",
        "//ortools/base",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "dynamic_permutation_test",
    srcs = ["dynamic_permutation_test.cc"],
    deps = [
        ":dynamic_permutation",
        "//ortools/base:gmock_main",
    ],
)

cc_library(
    name = "find_graph_symmetries",
    srcs = ["find_graph_symmetries.cc"],
    hdrs = ["find_graph_symmetries.h"],
    deps = [
        ":dense_doubly_linked_list",
        ":dynamic_partition",
        ":dynamic_permutation",
        ":sparse_permutation",
        "//ortools/base:murmur",
        "//ortools/graph",
        "//ortools/graph:iterators",
        "//ortools/graph:util",
        "//ortools/util:stats",
        "//ortools/util:time_limit",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base:log_severity",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/numeric:int128",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "find_graph_symmetries_test",
    srcs = ["find_graph_symmetries_test.cc"],
    deps = [
        ":dynamic_partition",
        ":dynamic_permutation",
        ":find_graph_symmetries",
        ":sparse_permutation",
        "//ortools/base:dump_vars",
        "//ortools/base:file",
        "//ortools/base:gmock_main",
        "//ortools/base:map_util",
        "//ortools/base:path",
        "//ortools/graph:io",
        "//ortools/graph:random_graph",
        "//ortools/graph:util",
        "@com_google_absl//absl/numeric:bits",
        "@com_google_absl//absl/random",
        "@com_google_absl//absl/random:distributions",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "binary_indexed_tree",
    hdrs = ["binary_indexed_tree.h"],
    deps = [
        "@com_google_absl//absl/log:check",
    ],
)

cc_test(
    name = "binary_indexed_tree_test",
    srcs = ["binary_indexed_tree_test.cc"],
    deps = [
        ":binary_indexed_tree",
        "//ortools/base:gmock_main",
    ],
)

cc_library(
    name = "n_choose_k",
    srcs = ["n_choose_k.cc"],
    hdrs = ["n_choose_k.h"],
    deps = [
        ":binary_search",
        "//ortools/base:mathutil",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/numeric:int128",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/time",
    ],
)

cc_test(
    name = "n_choose_k_test",
    srcs = ["n_choose_k_test.cc"],
    deps = [
        ":n_choose_k",
        "//ortools/base:dump_vars",
        "//ortools/base:gmock_main",
        "//ortools/base:mathutil",
        "//ortools/util:flat_matrix",
        "@com_google_absl//absl/numeric:int128",
        "@com_google_absl//absl/random",
        "@com_google_absl//absl/random:distributions",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_benchmark//:benchmark",
    ],
)
